package com.hfi.alibaba;

import lombok.Getter;
import lombok.Setter;

import java.time.LocalTime;

/**
 * @author ChangLiang
 * @date 2019/11/29
 */
@Getter
@Setter
public class TicketSeller {

    /**
     * 票量
     */
    private Integer inventory;

    private LocalTime closeTime;

    public boolean cloudSellAt(LocalTime localTime) {
        if (inventory > 0 && !localTime.isAfter(closeTime)) {
            return true;
        }
        return false;
    }

    public void sell(int count) {
        if (inventory >= count) {
            inventory = inventory - count;
        } else {
            throw new TicketException();
        }
    }

    public void refund(int count) {
        inventory = inventory + count;
    }

    class TicketException extends RuntimeException {
        public TicketException() {
            super("all ticket sold out");
        }
    }
}
